new[n] and delete every location with delete instead the whole chunk with delete[]
Posted
by pmr
on Stack Overflow
See other posts from Stack Overflow
or by pmr
Published on 2010-03-18T23:18:20Z
Indexed on
2010/03/18
23:21 UTC
Read the original article
Hit count: 855
c++
|memory-management
Is this valid C++ (e.g. not invoking UB) and does it achieve what I want without leaking memory? valgrinds complains about mismatching free and delete but says "no leaks are possible" in the end.
int main() {
int* a = new int[5];
for(int i = 0; i < 5; ++i)
a[i] = i;
for(int i = 0; i < 5; ++i)
delete &a[i];
}
The reason I'm asking: I have a class that uses boost::intrusive::list and I new every object that is added to that list. Sometimes I know how many objects I want to add to the list and was thinking about using new[] to allocate a chunk and still be able to delete every object on its own with the Disposer-style of boost::intrusive.
© Stack Overflow or respective owner